//
//  LockView.m
//  07-手势解锁
//
//  Created by J on 16/2/27.
//  Copyright © 2016年 J. All rights reserved.
//

#import "LockView.h"

@interface LockView ()
@property(nonatomic)NSString*n;
@property(nonatomic,strong)NSMutableArray* selectedButtons;
@property(nonatomic,assign)CGPoint point;
@end
@implementation LockView


- (NSMutableArray *)selectedButtons{
    if (!_selectedButtons) {
        self.selectedButtons = [NSMutableArray array];
    }
    return _selectedButtons;
}


//第一种方法 pan 事件处理

- (void)pan:(UIPanGestureRecognizer*)pan{
    
    //获取触摸点
    self.point = [pan locationInView:self];
    //判断触摸点是否在按钮上
    for (UIButton* button in self.subviews) {
      BOOL isContain =   CGRectContainsPoint(button.frame, self.point);
        if (isContain&&!button.selected) {//在按钮范围内同时不是处于选中状态
            button.selected = YES;
            [self.selectedButtons addObject:button];
        }
    }

    
    if (pan.state == UIGestureRecognizerStateEnded) {//手指抬起还原界面
        
        //创建可变字符串
        NSMutableString* mStr = [NSMutableString string];
        //保存输入密码
        for (UIButton* button in self.selectedButtons) {
            [mStr appendFormat:@"%zd",button.tag];
        }
        
        if ([mStr isEqualToString:@"123"]) {
            NSLog(@"解锁成功");

        }
        
        //还原界面
        
        //清除画线,把选中按钮清空
        [self.selectedButtons makeObjectsPerformSelector:@selector(setSelected:) withObject:NO];

        //取消所有选中点
        [self.selectedButtons removeAllObjects];
        
        
       
        
        
        
    }
    [self setNeedsDisplay];


}


//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self touchWithPotin:touches];
//    
//}
//
//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    
//    [self touchWithPotin:touches];
//
//    
//}
//
//
//
////第二种方法 touch事件处理
//
//- (void)touchWithPotin:(NSSet*)touches{
//    UITouch* touch = [touches anyObject];
//    CGPoint point = [touch locationInView:self];
//    self.point = point;
//    for (UIButton* button in self.subviews) {
//        BOOL isContain =   CGRectContainsPoint(button.frame, point);
//        if (isContain&&!button.selected) {//如果在button内并且处于选中状态
//            button.selected = YES;
//            [self.selectedButtons addObject:button];
//        }
//    }
//    
//    [self setNeedsDisplay];
//}
//
//- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    
//    //还原界面
//    //清除画线,把选中按钮清空
//    [self.selectedButtons makeObjectsPerformSelector:@selector(setSelected:) withObject:NO];
//    [self.selectedButtons removeAllObjects];
//    //取消所有选中点
//    [self setNeedsDisplay];
//}




//只要调用这个方法就会把之前的内容全部清除
- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    if (!self.selectedButtons.count){
        return;
    }
    //把所有选中的按钮中心连线
    UIBezierPath* path = [UIBezierPath bezierPath];
    
    NSUInteger  i = 0;
    for (UIButton * button in self.selectedButtons) {
            if (!i) {
                //设置起点
                [path moveToPoint:button.center];
                
            }else{
                [path addLineToPoint:button.center];
                
            }
            i++;
        
      
    }
    
    //连线到手指触摸点
    [path addLineToPoint:self.point];
    
    //所有的按钮都连接完毕
    [[UIColor orangeColor]set];
    path.lineWidth = 5;
    path.lineCapStyle = kCGLineCapRound;
    [path stroke];
    
    
}



//1
- (void)awakeFromNib{

    [super awakeFromNib];
    
    UIPanGestureRecognizer* tap = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:tap];
    //创建9个按钮
    for (int i =0; i<9; i++) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        [button setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [self addSubview:button];
    }
    
    
}
//2
- (void)layoutSubviews{
    [super layoutSubviews];
    NSUInteger count = self.subviews.count;
    NSUInteger cols = 3;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 74;
    CGFloat h = 74;
    CGFloat margin = (self.bounds.size.width - cols*w)/(cols+1);

    CGFloat col = 0;
    CGFloat row = 0;
    
    for (NSUInteger i = 0; i<count; i++) {
        UIButton* button = self.subviews[i];
        button.userInteractionEnabled = NO;//这样就不能达到高亮状态
        //当前按钮的列数
        col = i % cols;
        row = i/cols;
        x = margin + col* (w+margin);
        y = margin + row*(margin + h);

        button.frame = CGRectMake(x, y, w, h);
//        NSLog(@"%@",NSStringFromCGRect(button.frame));
        
    }
    
}





@end
